import { message } from 'antd';
import { login, checkUser, resetPassword } from '../service/index';

export default {

  namespace: 'gislogin',

  state: {
    test: ''
  },

  effects: {
    *login({ payload, callback }, { call, put, select }) {
      const res = yield call(login, payload);
      const { flag, data } = res.data;
      callback && callback(flag, data);
      if (!flag) return false;
      yield put({
        type: 'savaData',
        payload: {},
      })
    },
    *checkUser({ payload, callback }, { call, put }) {
      const res = yield call(checkUser, payload);
      try {
        if (res.data && res.data.flag) {
          return res.data.data.uid
        };
        return false;
      } catch (error) {
        message.error('服务器异常，请稍后再试!')
        return false;
      }
    },
    *resetPassword({ payload, callback }, { call, put }) {
      const res = yield call(resetPassword, payload);
      if (res.data) {
        return res.data
      }
      return false;
    }
  },

  reducers: {
    savaData(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    resetData(state, { payload }) {
      return {
        ...state,
        test: ''
      }
    }
  }

}
